﻿@model RXY_Mall.Application.Marketing.Dto.AddSubsidyInput

@section CSS{
    <link href="~/Content/css/layerui/layui.css" rel="stylesheet" />
}
<!-- Content Wrapper. Contains page content -->
<div class="content-wrapper">
    <section class="content-header">
        <h1>
            @ViewBag.text
            <small>补贴卡</small>
        </h1>
        <ol class="breadcrumb">
            <li><a href="#"><i class="fa fa-dashboard"></i> 首页</a></li>
            <li><a href="#">营销管理</a></li>
            <li><a href="/Marketing/Subsidies">补贴卡列表</a></li>
            <li class="active">@ViewBag.text 补贴卡</li>
        </ol>
    </section>
    <section class="content">
        <div class="row">
            <div class="col-md-12">
                <div class="box">
                    <div class="box-body">
                        <div class="ibox float-e-margins">

                            <div class="ibox float-e-margins">
                                <div class="ibox-content">
                                    <form class="form-horizontal" id="from-add" novalidate="novalidate" method="post" action="/Marketing/Subsidy">
                                        @Html.AntiForgeryToken()

                                        <div class="form-horizontal">
                                            <div class="form-group">
                                                @Html.LabelFor(model => model.subsidy_name, htmlAttributes: new { @class = "control-label col-md-2" })
                                                <div class="col-md-10">
                                                    @Html.EditorFor(model => model.subsidy_name, new { htmlAttributes = new { @class = "form-control" } })
                                                    @Html.ValidationMessageFor(model => model.subsidy_name, "", new { @class = "text-danger" })
                                                </div>
                                            </div>

                                            <div class="form-group">
                                                @Html.LabelFor(model => model.remark, htmlAttributes: new { @class = "control-label col-md-2" })
                                                <div class="col-md-10">
                                                    @Html.EditorFor(model => model.remark, new { htmlAttributes = new { @class = "form-control" } })
                                                    @Html.ValidationMessageFor(model => model.remark, "", new { @class = "text-danger" })
                                                </div>
                                            </div>

                                            <div class="form-group">
                                                @Html.LabelFor(model => model.amount, htmlAttributes: new { @class = "control-label col-md-2" })
                                                <div class="col-md-10">
                                                    @Html.EditorFor(model => model.amount, new { htmlAttributes = new { @class = "form-control" } })
                                                    @Html.ValidationMessageFor(model => model.amount, "", new { @class = "text-danger" })
                                                </div>
                                            </div>

                                            <div class="form-group">
                                                <label class="control-label col-md-2">补贴比例/期数</label>
                                                <div class="col-md-10">
                                                    <table id="tb" class="table table-bordered">
                                                        <tr id="trs">
                                                            <td>@Html.TextBoxFor(a => a.subsidy_items[0].rate, new { @class = "form-control", placeholder = "补贴比例" })</td>
                                                            <td>@Html.TextBoxFor(a => a.subsidy_items[0].total_cycle, new { @class = "form-control", placeholder = "补贴期数" })</td>
                                                            <td>@Html.TextBoxFor(a => a.subsidy_items[0].quantity, new { @class = "form-control", placeholder = "发行张数" })</td>
                                                            <td><button id="btnAdd" class="btn btn-primary" type="button">+增加</button></td>
                                                        </tr>
                                                        @{
                                                            if (Model.subsidy_items != null)
                                                            {
                                                                for (var i = 1; i < Model.subsidy_items.Count; i++)
                                                                {
                                                                    <tr id="trs">
                                                                        <td>@Html.TextBoxFor(a => a.subsidy_items[i].rate, new { @class = "form-control", placeholder = "补贴比例" })</td>
                                                                        <td>@Html.TextBoxFor(a => a.subsidy_items[i].total_cycle, new { @class = "form-control", placeholder = "补贴期数" })</td>
                                                                        <td>@Html.TextBoxFor(a => a.subsidy_items[i].quantity, new { @class = "form-control", placeholder = "发行张数" })</td>
                                                                        <td><button class="btn btn-danger" onclick="DeleteDiv(this)" type="button">-删除</button></td>
                                                                    </tr>
                                                                }
                                                            }
                                                        }
                                                    </table>
                                                </div>
                                            </div>    
                                            
                                            <div class="form-group">
                                                <label class="control-label col-md-2">有效期</label>
                                                <div class="col-md-10">
                                                    <div class="col-md-5" style="padding-left:0px">
                                                        <input type="text" class="form-control" id="start_time" name="start_time" value="@Model.start_time" placeholder="开始时间" />
                                                        @Html.ValidationMessageFor(model => model.start_time, "", new { @class = "text-danger" })
                                                    </div>
                                                    <div class="col-md-1" style="padding:5px; width:40px">
                                                        <label class="form-label">至</label>
                                                    </div>
                                                    <div class="col-md-5" style="padding-left:0px">
                                                        <input type="text" class="form-control" id="end_time" name="end_time" value="@Model.end_time" placeholder="结束时间" />
                                                        @Html.ValidationMessageFor(model => model.end_time, "", new { @class = "text-danger" })
                                                    </div>
                                                </div>
                                            </div>

                                            <div class="form-group">
                                                <label class="control-label col-md-2">参与补贴商品</label>
                                                <div class="col-md-10">
                                                    <button type="button" class="btn btn-info" id="btnAdd" onclick="OpenGoods('添加商品','/Marketing/GetGoods',1200,600)">
                                                        <i class="fa fa-plus"></i> 添加商品
                                                    </button>
                                                    <br />
                                                    <br />
                                                    @Html.EditorFor(model => model.goods_ids, new { htmlAttributes = new { @style = "width:330px;height:100px;" } })
                                                    @Html.ValidationMessageFor(model => model.goods_ids, "", new { @class = "text-danger" })
                                                </div>
                                            </div>

                                            <div class="form-group">
                                                <div class="col-md-2"></div>
                                                <div class="col-md-10">
                                                    <label id="total_amount"></label>
                                                </div>
                                            </div>

                                            @if (Model.subsidy_id == 0)
                                            {
                                                <div class="form-group">
                                                    <div class="col-sm-4 col-sm-offset-3">
                                                        <button class="btn btn-primary" type="submit">保存</button>
                                                    </div>
                                                </div>
                                            }
                                        </div>
                                    </form>
                                </div>
                            </div>
                        </div>
                    </div>
                </div>
            </div>
        </div>
    </section>

</div>

@section JS{
    <script src="~/Content/js/jquery.form.js"></script>
    <script src="~/Content/plugins/validate/jquery.validate.min.js"></script>
    <script src="~/Content/plugins/validate/messages_zh.min.js"></script>
    <script type="text/javascript">

        $('#start_time').datepicker({
            autoclose: true
        });

        $('#end_time').datepicker({
            autoclose: true
        });

        function ComputeAmount(obj) {

        }

        $("#btnAdd").on("click", function () {
            var trLen = $("#tb tr[id='trs']").length;
            var $lastTr = $("#tb tr[id='trs']").last();
            var tr = "<tr id='trs'>";
            tr += "<td><input type='text' name='subsidy_items[" + trLen + "].rate' class='form-control' placeholder='补贴比例' /></td>";
            tr += "<td><input type='text' name='subsidy_items[" + trLen + "].total_cycle' class='form-control' placeholder='补贴期数' /></td>";
            tr += "<td><input type='text' name='subsidy_items[" + trLen + "].quantity' class='form-control' placeholder='发行张数' /></td>";
            tr += "<td><button class='btn btn-danger' onclick='DeleteDiv(this)' type='button'>-删除</button></td>"
            tr += "</tr>";
            $(tr).insertAfter($lastTr);
        });

        function AddDiv(object) {
            var couponValue = $('#couponValue').parent();
            var couponValueClone = $('#couponValue').html();
            couponValue.append(couponValueClone);
        }

        function DeleteDiv(object) {
            $(object).parent().parent().remove();
        }

        function OpenGoods(title, url, w, h) {
            var goods_ids = $('#goods_ids').val();
            if (goods_ids.length > 0) {
                url = url + "?goods_ids=" + goods_ids;
            }

            layer_show(title, url, w, h);
        }

        /*弹出层*/
        /*
            参数解释：
            title	标题
            url		请求的url
            id		需要操作的数据id
            w		弹出层宽度（缺省调默认值）
            h		弹出层高度（缺省调默认值）
        */
        function layer_show(title, url, w, h) {
            if (title == null || title == '') {
                title = false;
            };
            if (url == null || url == '') {
                url = "404.html";
            };
            if (w == null || w == '') {
                w = 800;
            };
            if (h == null || h == '') {
                h = ($(window).height() - 50);
            };

            layer.open({
                type: 2,
                title: title,
                fixed: false, //不固定
                maxmin: true,
                shadeClose: true,
                shade: 0.4,
                area: [w + 'px', h + 'px'],
                content: url,
                btn: ['确定', '关闭'],
                yes: function (index, layero) {
                    //当点击‘确定’按钮的时候，获取弹出层返回的值
                    var res = $(layero).find("iframe")[0].contentWindow.callbackdata();
                    //打印返回的值，看是否有我们想返回的值。
                    $('#goods_ids').val(res);
                    //最后关闭弹出层
                    layer.close(index);
                },
                cancel: function (index) {
                    layer.close(index);
                    //右上角关闭回调
                }
            });

        }

    </script>
}